Toast MessageBox

Toast를 통해서 메세지 박스등 기능을 하는 위젯을 팝업

toast될 위젯은 /app/res/layout/ 디렉토리에 별도로 생성하고,
Java를 통해서 연동해주어야 함(R.layout.*xml)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIALOG BOX" />
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
AlertDialog.Builder dlg=new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("TITLE!!");
dlg.setMessage("CONTENT!!!");
dlg.setIcon(R.drawable.ic_launcher);
dlg.setPositiveButton("CONFIRM", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i){
Toast.makeText(MainActivity.this, "Got CONFIRM", Toast.LENGTH_SHORT).show();
}
});
dlg.show();
}
});
}
}
setItems()
목록 대화 상자
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
final String[] versionArray=new String[] {"", "(10)", "(11)"};
AlertDialog.Builder dlg=new AlertDialog.Builder(MainActivity.this);
dlg.setTitle(" ?");
dlg.setIcon(R.drawable.q10);
dlg.setItems(versionArray, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
button1.setText(versionArray[i]);
}
});
dlg.setPositiveButton("", null);
dlg.show();
}
});
}
}
setSigleChoiceItems()
라디오 버튼 목록 대화상자
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
final String[] versionArray=new String[] {"", "(10)", "(11)"};
AlertDialog.Builder dlg=new AlertDialog.Builder(MainActivity.this);
dlg.setTitle(" ?");
dlg.setIcon(R.drawable.q10);
dlg.setSingleChoiceItems(versionArray, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
button1.setText(versionArray[i]);
}
});
dlg.setPositiveButton("", null);
dlg.show();
}
});
}
}
setMultiChoiceItems()
체크박스 목록 대화상자
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
final String[] versionArray=new String[] {"", "(10)", "(11)"};
final boolean[] checkArray=new boolean[] {true, false, false};
AlertDialog.Builder dlg=new AlertDialog.Builder(MainActivity.this);
dlg.setTitle(" ?");
dlg.setIcon(R.drawable.q10);
dlg.setMultiChoiceItems(versionArray, checkArray, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
button1.setText(versionArray[i]);
}
});
dlg.setPositiveButton("", null);
dlg.show();
}
});
}
}
사용자 정보 입력 대화 상자
xml 3개를 생성한 후 java를 이용해서 xml 화면 출력
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textSize="20dp" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</LinearLayout>
res/layout/dialog1.xml
(LinearLayout_Root element 으로 추가로 생성)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android:="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textSize="20dp" />
<EditText
android:id="@+id/dlgEdt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp" />
<EditText
android:id="@+id/dlgEdt2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
res/layout/toast1.xml
(LinearLayout_Root element 으로 추가로 생성)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_star_big_on" />
<TextView
android:id="@+id/toastText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_star_big_on" />
</LinearLayout>
MainActivity.java
package com.example.myapplication
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView tvName, tvEmail;
Button button;
EditText dlgEdtName, dlgEdtEmail;
TextView toastText;
View dialogView, toastView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInsatnceState);
setContentView(R.layout.activity_main);
setTitle(" ");
tvName=(TextView)findViewById(R.id.tvName);
tvEmail=(TextView)findViewById(R.id.tvEmail);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
// Dialog (dialog1.xml)
dialogView=(View)View.inflate(MainActivity.this, R.layout.dialog1, null);
AlertDialog.Builder dlg=new AlertDialog.Builer(MainActivity.this);
dlg.setTitle(" ");
dlg.setIcon(R.drawable.ic_menu_allfriends);
dlg.setView(dialogView);
dlg.setPositiveButton("", new DialogInterface.onClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i){
// dialog EditText java
dlgEdtName=(EditText)dialogView.findViewById(R.id.dlgEdt1);
dlgEdtEmail=(EditText)dialogView.findViewById(R.id.dlgEdt2);
tvName.setText(dlgEdtName.getText().toString());
tvEmail.setText(dlgEdtEmail.getText().toString());
}
});
dlg.setNegativeButton("", new DialogInterface.onClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i){
// Toast (toast1.xml);
Toast toast=new Toast(MainActivity.this);
toastView=(View)View.inflate(MainActivity.this, R.layout.toast1, null);
// toast TextView java
toastText=(TextView)toastView.findViewById(R.id.toastText1);
toastText.setText(".");
toast.setView(toastView);
toast.show();
});
dlg.show();
}
}
});
}
}